﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Noria.Web.MyMSDN.Models;

namespace Noria.Web.Test
{
    [TestClass]
    public class MmContextTest
    {
        [TestMethod]
        public void LoadContext()
        {
            using (var db = new MmContext())
            {
                db.Categories.Add(new Category
                {
                    CategoryId = 11,
                    Name = "Test...",
                    Families = new List<Family>
                        {
                            new Family
                                {
                                     FamilyId = 15,
                                     Name = "测试",

                                     Files = new List<File>()
                                         {
                                             new File
                                                 {
                                                     FileId = 155,
                                                     Description = "11",
                                                     DisplaySize = "123",
                                                     FileName = "11",
                                                     Locale = "en",
                                                     PostedDate = DateTime.Now,
                                                     Sha1 = "123sha1",
                                                 },

                                         }
                                }, 
                            new Family
                                {
                                     FamilyId = 16,
                                     Name = "Great Wall"
                                }
                        }
                });

                //var cat = new Category
                //    {
                //        CategoryId = 1,
                //        Name = "目录1",
                //    };
                //cat.Families = new List<Family>();
                //cat.Families.Add(new Family
                //    {
                //        FamilyId = 120,
                //        Name = "家族2"
                //    });

                //db.Categories.Add(cat);
                db.SaveChanges();
            }
        }

        [TestMethod]
        public void HistoryTest()
        {
            using (var db = new MmContext())
            {
                History his = new History
                    {
                        Start = DateTime.UtcNow,
                        Initiator = "msdn"
                    };
                db.Histories.Add(his);
                db.SaveChanges();

                his.End = DateTime.UtcNow.AddHours(1);
                db.SaveChanges();

                his.Inserted = 180;
                db.SaveChanges();

                var time = DateTime.UtcNow.AddHours(-10);
                var query = from t in db.Histories
                            where t.Start > time
                            select t;
                var cnt = query.Count();
                Console.WriteLine(cnt);

            }
        }

        [TestMethod]
        public void ChecksumTest()
        {
            using (var db = new MmContext())
            {
                Checksum chk = new Checksum
                {
                    FileName = "34234地方",
                    Sha1 = "DE63CE2D7C795D0013C4121BB62352AD77AD3B33",
                    Size = 477663,
                    eMule = "D1D032D20568F9ED16F072075F4E9961",
                    SubmittedDate = DateTime.Now,
                    Initiator = "127.0.0.1",
                };
                db.Checksum.Add(chk);
                db.SaveChanges();

                var time = DateTime.UtcNow.AddHours(-10);
                var query = from t in db.Checksum
                            where t.SubmittedDate > time
                            select t;
                var cnt = query.Count();
                Console.WriteLine(cnt);

            }
        }

        [TestMethod]
        public void SyncChecksumTest()
        {
            using (var db = new MmContext())
            {
                int s = db.SyncChecksum();
            }
        }

        [TestMethod]
        public void ExportFileMethod()
        {
            using (var db = new MmContext())
            {
                var files = db.Files.ToArray();
                for (int i = 0; i < 10; i++)
                {
                    var begin = i * 2000;
                    var end = Math.Min((i + 1) * 2000, files.Length);

                    var jArray = JArray.FromObject(files.Skip(begin).Take(end - begin));
                    var buffer = jArray.ToString(Formatting.None);
                    System.IO.File.WriteAllText(i + ".json", buffer);
                }
            }
        }

    }
}
